
package com.hlkj.pay.service.merchant.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.pay.dto.merchant.MerchantInfoQueryDto;
import com.hlkj.pay.infrastructure.mapper.merchant.AgentInfoMapper;
import com.hlkj.pay.infrastructure.model.merchant.AgentInfoDO;
import com.hlkj.pay.service.CommonSnFilterService;
import com.hlkj.pay.service.merchant.IAgentService;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

/**
 * @author HlpayTeam
 * @date 2024/09/12 13:43
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class AgentServiceImpl extends CommonSnFilterService implements IAgentService {

    private final AgentInfoMapper agentInfoMapper;

    @Override
    public AgentInfoDO queryAgent(String sn) {
        MerchantInfoQueryDto merchantInfoQueryDto = new MerchantInfoQueryDto();
        merchantInfoQueryDto.setSn(sn);
        return queryOne(merchantInfoQueryDto);
    }

    @Override
    public void updateAgentInfoBySn(AgentInfoDO merchantInfoDO) {
        MerchantInfoQueryDto merchantInfoQueryDto = new MerchantInfoQueryDto();
        merchantInfoQueryDto.setSn(merchantInfoDO.getSn());
        agentInfoMapper.update(merchantInfoDO, buildWrapper(merchantInfoQueryDto));
    }

    @Override
    public void addAgentInfo(AgentInfoDO agentInfoDO) {
        agentInfoMapper.insert(agentInfoDO);
    }

    @Override
    public void delAgentInfo(String sn) {
        MerchantInfoQueryDto merchantInfoQueryDto = new MerchantInfoQueryDto();
        merchantInfoQueryDto.setSn(sn);
        agentInfoMapper.delete(buildWrapper(merchantInfoQueryDto));
    }

    AgentInfoDO queryOne(MerchantInfoQueryDto merchantInfoQueryDto) {
        LambdaQueryWrapper<AgentInfoDO> queryWrapper = buildWrapper(merchantInfoQueryDto);
        queryWrapper.last("limit 1");
        return agentInfoMapper.selectOne(queryWrapper);
    }

    /**
     * 查询条件封装
     *
     * @param merchantInfoQueryDto
     * @return
     */
    LambdaQueryWrapper<AgentInfoDO> buildWrapper(MerchantInfoQueryDto merchantInfoQueryDto) {
        filterSn(merchantInfoQueryDto);
        LambdaQueryWrapperX<AgentInfoDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(AgentInfoDO::getSn, merchantInfoQueryDto.getSn());
        return queryWrapper;
    }

}
